package com.xy.modular.basics.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.pager.SearchCondition;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.basics.entity.Dep;
import com.xy.modular.basics.service.IDepService;
import com.xy.modular.sys.action.ModuleController;
import com.xy.modular.sys.entity.Module;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/dep")
public class DepController extends BaseController {

	static Logger log = LoggerFactory.getLogger(ModuleController.class);

	@Resource
	private IDepService depService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject list(HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.setColumns(" basics_dep.*,sys_tenant.tenant_name ");
		pageRequest.setLeftJoinSql(" left join  basics_dep.tenant_id=sys_tenant.id ");
		return new MReturnObject(MReturnObject.SUCCESS, depService.findColumnsPageAll(pageRequest));
	}

/*	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/{depId}/user/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject userDepIdList(HttpServletRequest request, @PathVariable String depId) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(User.DEPID, SearchCondition.EQUAL, depId);
		pageRequest.setColumns(" basics_user.*, basics_dep.name depName");
		pageRequest.setLeftJoinSql(
				" left join basics_user_dep on  basics_user_dep.user_id=basics_user.id   LEFT JOIN basics_dep ON basics_user_dep.dep_id = basics_dep.id");
		return new MReturnObject(MReturnObject.SUCCESS, depService.findColumnsAll(pageRequest));
	}*/

	@RequestMapping(value = "/treeDep", method = RequestMethod.GET)
	public @ResponseBody MReturnObject getTreeDep(HttpServletRequest request, Dep dep) {
		PageRequest pageRequest = newPageRequest(request);
		if (dep.getIsValid() != null && !dep.getIsValid().equals("")) {
			pageRequest.addAndCondition(Dep.NAME, SearchCondition.LIKE, "%" + dep.getName() + "%");
		} else {
			pageRequest.addAndCondition(Dep.SUPERMOD, SearchCondition.EQUAL, "0");
		}
		if (dep.getIsValid() != null && !dep.getIsValid().equals("")) {
			pageRequest.addAndCondition(Dep.ISVALID, SearchCondition.EQUAL, dep.getIsValid());
		} else {
			pageRequest.addAndCondition(Dep.SUPERMOD, SearchCondition.EQUAL, "0");
		}
		return new MReturnObject(MReturnObject.SUCCESS, depService.getTreeDep(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(Dep dep) {
		try {
			depService.validateEntity(dep);
			dep.setId(UUIDUtils.UUID());
			depService.save(dep);
		} catch (Exception e) {
			log.info("菜单保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, dep);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(Dep dep) {
		try {
			depService.validateEntity(dep);
			depService.update(dep);
		} catch (Exception e) {
			log.info("菜单保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, dep);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(Module module, @PathVariable String id) {
		try {
			depService.deleteById(id);
		} catch (Exception e) {
			log.info("菜单删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
